package com.bml.www.teacher.common.http;

import android.content.Context;

import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.RequestListener;
import com.pachong.android.frameworkbase.utils.SLog;
import com.bml.www.teacher.account.AccountManager;
import com.bml.www.teacher.common.http.*;
import com.bml.www.teacher.common.http.Authorization;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * My http request class, extends HttpRequest
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/7/18 15:10
 * @Version: 1.0.0
 */
public class MyHttpRequest extends HttpRequest {
    public static final String TAG = MyHttpRequest.class.getSimpleName();

    public MyHttpRequest(Context context) {
        super(context);
    }


    @Override
    public void get(String url, UrlParams params, RequestListener cb) {
        super.get(url, params, makeHeaderParamMap(UrlParams.getUrlWithQueryString(url, params), "get", null), cb);
    }


    @Override
    public void get(String url, UrlParams params, Map<String, String> headParams, RequestListener cb) {
        super.get(url, params, makeHeaderParamMap(UrlParams.getUrlWithQueryString(url, params), "get", headParams), cb);
    }

    public static void get(Context context, String url, UrlParams params, RequestListener cb) {
        IHttpRequest request = new MyHttpRequest(context);
        request.get(url, params, cb);
    }

    public static void get(Context context, String url, UrlParams params, Map<String, String> headParams, RequestListener cb) {
        IHttpRequest request = new MyHttpRequest(context);
        request.get(url, params, headParams, cb);
    }

    @Override
    public void put(String url, JSONObject jsonObject, Map<String, String> headParams, RequestListener cb) {
        super.put(url, jsonObject, makeHeaderParamMap(url, "put", headParams), cb);
    }


    @Override
    public void put(String url, JSONObject jsonObject, RequestListener cb) {
        super.put(url, jsonObject, makeHeaderParamMap(url, "put", null), cb);
    }

    @Override
    public void post(String url, UrlParams params, final RequestListener cb){
        super.post(url, params, makeHeaderParamMap(url, "post", null), cb);
    }

    @Override
    public void post(String url, UrlParams params, Map<String, String> headParams, final RequestListener cb){
        super.post(url, params, makeHeaderParamMap(url, "post", headParams), cb);
    }

    @Override
    public void post(String url, JSONObject jsonObject, final RequestListener cb) {
        super.post(url, jsonObject, makeHeaderParamMap(url, "post", null), cb);
    }

    @Override
    public void post(String url, JSONObject jsonObject, Map<String, String> headParams, final RequestListener cb){
        super.post(url, jsonObject, makeHeaderParamMap(url, "post", headParams), cb);
    }


    @Override
    public void delete(String url, UrlParams params, RequestListener cb) {
        super.delete(url, params, makeHeaderParamMap(UrlParams.getUrlWithQueryString(url, params), "delete", null), cb);
    }


    @Override
    public void delete(String url, UrlParams params, Map<String, String> headParams, RequestListener cb) {
        super.delete(url, params, makeHeaderParamMap(UrlParams.getUrlWithQueryString(url, params), "delete", headParams), cb);
    }


    private Map<String, String> makeHeaderParamMap(String url, String method, Map<String, String> headerMap) {
        Map<String, String> retParamMap = new HashMap<>();
        if (headerMap != null && headerMap.size() > 0) {
            retParamMap.putAll(headerMap);
        }

        if (AccountManager.isTokenValid()) {
            retParamMap.putAll(makeAuthMap(url, method));
        }
        return retParamMap;
    }


    private Map<String, String> makeAuthMap(String url, String method) {
       TokenInfo tokenInfo = AccountManager.getTokenInfo();

        String authorization = Authorization.createAuthorization(tokenInfo, url, method);



        SLog.e(TAG, "makeAuthMap  get user info auto === " + authorization);
        Map<String, String> headParamMap = new HashMap<>();
        headParamMap.put("Authorization", authorization);
        return headParamMap;
    }


}
